$(function(){
    /**
     * 分页初始条件
     */
    var total_count = 1;//分页总记录数
    var total = 1;//分页总页面数
    var currentPage = 1;//当前页
    var pageSize = pageOption.pageSize;//每页显示的记录数
    var idList = [];//批量选择id所存的数组
    var root_url = $("#root_url").val();

    /**
     * 页面初始化
     */
    function init(){
        myPagination();
        bindEvent();
    }

    /**
     * 事件绑定
     */
    function bindEvent(){
        //根据条件查询订单信息
        $('.inner-section #search_btn').click(function() {
            render(true,1,pageSize);
            return false;
        });

        //enter键盘事件
        $(".inner-section .search-param-form input").keydown(function(event){
            event = event ? event: window.event;
            if(event.keyCode == 13){
                render(true,1,pageSize);
                return false;
            }
        });
    }

    /**
     * 获取模糊参数
     */
    function getSelectInfo(){
        var selectInfo = {};
        $.each($('#search-param-form').serializeArray(), function(i, field){
            selectInfo[field.name]=field.value;
        });
        JSON.stringify(selectInfo);
        return selectInfo;
    }

    /**
     * 分页显示方法
     */
    function myPagination(){
        render(true,1,pageSize);
        //调用公共分页方法
        pagination("#page-selection",{pageSize:pageSize,total:total},render);

    }

    /**
     * 分页动态渲染数据
     * @param async ajax请求是否异步
     * @param pageIndex 当前显示页
     * @param pageSize 每页显示记录数
     */
    function render(async,pageIndex,pageSize){
        var selectInfo = getSelectInfo();
        selectInfo.pageIndex = pageIndex;
        selectInfo.pageSize = pageSize;
        $.ajax({
            async:async,
            type:'post',
            url:'pagingOrder',
            data:selectInfo,//从1开始计数
            dataType:'json',
            success:function(result){
                var html ='';
                if(result.errorCode == 0){
                    total = result.data.pageInfo.total_page;
                    total_count = result.data.pageInfo.total_count;
                    $("#page-selection").bootpag({total:total,total_count:total_count});//重新计算总页数,总记录数

                    currentPage = result.data.pageInfo.current_page;
                    var myList = result.data.dataList;

                    html+='<tr>' +
                        '<th class="th1">序号</th>' +
                        '<th class="" style="width: 180px;">单号</th>' +
                        '<th class="">姓名</th>' +
                        '<th class="">被浏览用户</th>' +
                        '<th class="">订单金额</th>' +
                        '<th class="">支付金额</th>' +
                        '<th class="">支付时间</th>' +
                        '<th class="">状态</th>' +
                        '<th class="">微信单号</th>' +
                        '<th class="">创建时间</th>' +
                        '<th class="">操作</th>' +
                        '</tr>';
                    var colspan = $(html).find("th").length;
                    for(var i = 0; i < myList.length;i++){
                        var obj = myList[i];
                        var num = (pageIndex-1)*pageSize + i+1;
                        var id = obj.id;
                        var code = obj.code;
                        var order_id = obj.order_id;
                        var user_id = obj.user_id;
                        var username = obj.username;
                        var nick_name = obj.nick_name;
                        var like_user = obj.like_user;
                        var amount = obj.amount;
                        var pay_amount = obj.pay_amount||'--';
                        var pay_time = obj.pay_time||'--';
                        var state = obj.state;
                        var delivery_state = obj.delivery_state;
                        var transaction_id = obj.transaction_id||'--';
                        var store_id = obj.store_id||'--';
                        var add_time = obj.add_time||'--';
                        var identity_text = obj.identity_text;
                        var checked = (idList.indexOf(id) >= 0) ? "checked":"";//判断当前记录先前有没有被选中

                        var state_text = '待支付';
                        if (state == 1) state_text = '已支付';

                        var pay_html = ''
                        if (state == 0) {
                            pay_html = '<a href="javascript:;" class="pay" data-id="' + order_id + '" style="color: red;">模拟支付</a>'
                        } else {
                            pay_html = '<a href="javascript:;" class="distribute" data-id="' + order_id + '">分销详情</a>';
                        }

                        var identity_text_html = '<p style="color: gray;font-size: 12px;">('+identity_text+')</p>';

                        html += '<tr>'
                            + '<td class="th1">' + num + '</td>'
                            + '<td class="">' + code + '</td>'
                            + '<td class="">' + (username == null ? nick_name : username) + identity_text_html + '</td>'
                            + '<td class="">' + (like_user != null ? like_user.username : '--') + '</td>'
                            + '<td class="">' + amount + '</td>'
                            + '<td class="">' + pay_amount + '</td>'
                            + '<td class="">' + pay_time + '</td>'
                            + '<td class="">' + state_text + '</td>'
                            + '<td class="">' + transaction_id + '</td>'
                            + '<td class="">' + add_time + '</td>'
                            + '<td class="">'
                            + pay_html
                            + '</td>'
                            + '</tr>';
                    }

                    if(myList.length == 0){
                        html += '<tr><td colspan="'+colspan+'"><p class="text-danger">暂无数据。</p></td></tr>';
                        $("#list-table tbody").html(html);
                    }else{
                        $("#list-table tbody").html(html);
                        myCheck();
                        batchSelect(idList,".inner-section #list-table .select-all",".inner-section #list-table .select-single");

                        //假支付
                        $('.pay').click(falsePay);
                        //分销详情信息
                        $('.distribute').click(getUserDistributeInfo);
                    }
                }else{
                    responseTip(result.errorCode,result.errorInfo,1500);
                }
            },
            error:errorResponse
        });
    }

    // 分销详情信息
    function getUserDistributeInfo () {
        let id = $(this).attr('data-id');
        layer.open({
            type: 2,
            title:'分销详情',
            area: ['550px', '400px'],
            content: '../Order/getUserDistributeInfo?id='+id
        });
    }

    function falsePay() {
        var id = $(this).attr('data-id');
        layer.confirm('调试测试支付', {
            title: '提示',
            btn: ['确认支付', '取消'],
            btn1: function (index, layero) {
                layer.closeAll();
                $.ajax({
                    url: "notifyByOneYuanOrder",
                    type: "post",
                    data: {"id": id},
                    dataType: "json",
                    success: function (json, statusText) {
                        if (json.errorCode == 0) {
                            if (currentPage != 1 && total_count % pageSize == 1) {//非首页且末页记录数为1时
                                currentPage = currentPage - 1;
                            }
                            render(true, currentPage, pageSize);
                        } else {
                            responseTip(json.errorCode, json.errorInfo, 1500);
                        }
                    }
                });
            },
            btn2: function (index) {

            }
        })
    }
    
    init();
});